<?php

class mysql{
    //查询并统计相同ip的用户
    public function userIp()
    {
        $where = $this->request->getMore([
            [['ip', 's'], ''],
            [['page', 'd'], 1],
            [['limit', 'd'], 10],
            [['type', 'd'], 1],
            [['count', 'd'], 0]
        ]);

        if ($where['type'] == 1){
            $ip_name = 'add_ip';
        }else{
            $ip_name = 'last_ip';
        }

        $sql = 'select '.$ip_name.' as ip,GROUP_CONCAT(phone) as phones,count(uid) as count from eb_user';
        if (!empty($where['ip'])){
            $sql .=' WHERE '.$ip_name.' = \''.$where['ip'].'\'';
        }
        $sql.=' group by '.$ip_name;
        if (!empty($where['count'])){
            $sql.= ' having count>'.$where['count'];
        }
        $sql.=' ORDER BY count desc LIMIT '.($where['page']-1)*$where['limit'].','.$where['limit'].';';

        $data = Db::query($sql);

        foreach ($data as &$val){
            $val['phones'] = explode(',',$val['phones']);
        }
        return app('json')->success($data);
    }
}
